package com.leetcode.problems019;


import com.leetcode.util.ListNode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Date 2021/11/20 14:34
 * @Author ghery
 * @Version V1.0
 */
public class Solution {


    public ListNode removeNthFromEnd(ListNode head, int n) {
        if(n == 0 ){
            return head;
        }
        ListNode dummyHead = new ListNode(0,head);
        int i = 0;
        ListNode cur = dummyHead;
        while(i <= n && cur != null){
            cur = cur.next;
            i++;
        }
        if(i <= n){
            //表示n超过了节点的长度，不进行删除操作
            return dummyHead.next;
        }
        ListNode first =dummyHead;
        ListNode second = cur;
        while(second != null){
            first = first.next;
            second = second.next;
        }
        first.next = first.next.next;
        return dummyHead.next;
    }
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5};
        ListNode.printListNode(new Solution().removeNthFromEnd(ListNode.getListFormArray(arr),6));
    }
}
